package com.javabi.htmlbuilder.html.element;

import java.util.Collection;

import com.javabi.htmlbuilder.css.CSSBuilder;
import com.javabi.htmlbuilder.css.CSSDeclarationSet;
import com.javabi.htmlbuilder.css.CSSStyle;
import com.javabi.htmlbuilder.css.CSSStyleList;
import com.javabi.htmlbuilder.css.selector.SimpleSelector;
import com.javabi.htmlbuilder.html.HTMLBuilder;
import com.javabi.htmlbuilder.html.HTMLElement;
import com.javabi.htmlbuilder.html.HTMLElementName;

/**
 * An HTML Style Set Element.
 * <p>
 * &lt;style&gt; ... &lt;/style&gt;
 * </p>
 */
public class StyleSet extends HTMLElement<StyleSet> {

	/** The style list. */
	private CSSStyleList styleList = new CSSStyleList();

	/**
	 * Creates a new style.
	 */
	public StyleSet() {
	}

	/**
	 * Returns the style
	 * @return the style
	 */
	public CSSStyleList getList() {
		// ESCA-JAVA0259: allow return of raw list, not thread safe
		return styleList;
	}

	/**
	 * Returns the declarations for a simple selector.
	 * @param selectors the selectors.
	 * @return the declarations.
	 */
	public CSSDeclarationSet declarations(String... selectors) {
		CSSStyle style = new CSSStyle();
		style.addSelector(new SimpleSelector(selectors));
		styleList.add(style);
		return style.declarations();
	}

	/**
	 * Returns the declarations for a simple selector.
	 * @param selectors the selectors.
	 * @return the declarations.
	 */
	public CSSDeclarationSet declarations(Collection<String> selectors) {
		CSSStyle style = new CSSStyle();
		style.addSelector(new SimpleSelector(selectors));
		styleList.add(style);
		return style.declarations();
	}

	/**
	 * Returns the declarations for a simple selector.
	 * @param selector the selector.
	 * @return the declarations.
	 */
	public CSSDeclarationSet declarations(String selector) {
		CSSStyle style = new CSSStyle();
		style.addSelector(new SimpleSelector(selector));
		styleList.add(style);
		return style.declarations();
	}

	/**
	 * Returns true to format this element.
	 * @return true to format this element.
	 */
	public boolean format() {
		return true;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.STYLE;
	}

	/**
	 * Append this to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendTo(HTMLBuilder html) {
		html.open(this, false);
		CSSBuilder css = new CSSBuilder();
		for (CSSStyle style : styleList) {
			style.appendTo(css);
		}
		html.css(css);
		html.close(this);
		return html;
	}

}
